﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data.Entity;
using System.Threading.Tasks;

namespace rmyy.Repository
{
    public partial class BaseRepository<T> where T:class,new()
    {
        DbContext db = DBContextFactory.CreateDBContext();
        public IQueryable<T> LoadEntities(Expression<Func<T,bool>> whereLambda)
        {
            return db.Set<T>().Where(whereLambda);
        }
        public IQueryable<T> LoadPageEntities<s>(int pageIndex,int pageSize,out int totalCount,Expression<Func<T,bool>> whereLambda,Expression<Func<T,s>> orderLambda,bool isAsc)
        {
            var temp = db.Set<T>().Where(whereLambda);
            totalCount = temp.Count();
            temp = isAsc ? temp.OrderBy(orderLambda) : temp.OrderByDescending(orderLambda);
            return temp.Skip(pageIndex * pageSize).Take(pageSize);
        }
        public T AddEntity(T entity)
        {
            return db.Set<T>().Add(entity);
        }
        public void DelEntity(T entity)
        {
            db.Entry<T>(entity).State = EntityState.Deleted;          
        }
        public void EditEntity(T entity)
        {
            db.Entry<T>(entity).State = EntityState.Modified;
        }
    }
}
